import UiPointer from "../Game/UI/UiPointer";
import MoveComp from "../Game/funcComp/MoveComp";
import { Location } from "../Game/info/Predefine";
import PlayerManager from "../Game/manager/PlayerManager";
import UiManager from "../Game/manager/UiManager";
import Player from "../Game/player/Player";
import Scene from "../Scene";
import { DirectionCode, KeyEventParameters, SceneState, TButton, TLayout } from "../SceneControl";

const { ccclass, property, menu } = cc._decorator;

@ccclass
@menu("Scene/GameScene")
export default class GameScene extends Scene {

    keyDown: (para: KeyEventParameters) => void;

    keyUp: (para: KeyEventParameters) => void;

    longPressTime: number;

    protected start(): void {
        this.initScene();
        PlayerManager.instance.controllerPlayer.node.on(Player.EventType.安家, this.checkExist, this);
    }

    public initScene() {
        let layout = new TLayout();
        let tbtn = new TButton(this.node)
        layout.buttonList = [[tbtn]];
        tbtn.addKeyDownEvent(this._keyDownOnPlayer.bind(this));
        tbtn.addKeyUpEvent(this._keyUpOnPlayer.bind(this));
        this.tscene.sceneState = SceneState.hideFinger;
        this.tscene.addLayout(layout);
        this.tscene.joinController();
    }

    /**
     * 玩家安家时，进入基地状态
     * 
     */
    checkExist() {
        let tbtn = new TButton(this.node)
        tbtn.addKeyDownEvent(this._keyDownOnGame.bind(this));
        tbtn.addKeyLongPressEvent(this._keyLongPressOnGame.bind(this));
        tbtn.addKeyUpEvent(this._keyUpOnGame.bind(this));
        this.tscene.currLayout.buttonList = [[tbtn]];
        this.tscene.apply();
    }

    _keyLongPressOnGame(para: KeyEventParameters, dt: number) {
        let oldLocation = UiPointer.instance.currLocation;
        let newLocation: Location;
        switch (para.DirectionCode) {
            case DirectionCode.LEFT:
                newLocation = { locationX: oldLocation.locationX - 1, locationY: oldLocation.locationY };
                break;
            case DirectionCode.RIGHT:
                newLocation = { locationX: oldLocation.locationX + 1, locationY: oldLocation.locationY };
                break;
            case DirectionCode.UP:
                newLocation = { locationX: oldLocation.locationX, locationY: oldLocation.locationY - 1 };
                break;
            case DirectionCode.DOWN:
                newLocation = { locationX: oldLocation.locationX, locationY: oldLocation.locationY + 1 };
                break;
        }

        if (newLocation) {
            this.longPressTime += dt;
            if (this.longPressTime < 0.55) return;
            this.longPressTime = 0.5;
            UiPointer.instance.currLocation = newLocation;
        }
    }

    _keyDownOnGame(para: KeyEventParameters) {
        let oldLocation = UiPointer.instance.currLocation;
        switch (para.DirectionCode) {
            case DirectionCode.LEFT:
                UiPointer.instance.currLocation = { locationX: oldLocation.locationX - 1, locationY: oldLocation.locationY };
                break;
            case DirectionCode.RIGHT:
                UiPointer.instance.currLocation = { locationX: oldLocation.locationX + 1, locationY: oldLocation.locationY };
                break;
            case DirectionCode.UP:
                UiPointer.instance.currLocation = { locationX: oldLocation.locationX, locationY: oldLocation.locationY - 1 };
                break;
            case DirectionCode.DOWN:
                UiPointer.instance.currLocation = { locationX: oldLocation.locationX, locationY: oldLocation.locationY + 1 };
                break;
            case DirectionCode.ENTER:
                UiManager.instance.openLocationDialog(UiPointer.instance.currLocation);
                break;
            case DirectionCode.BACK:
                UiManager.instance.openPauseDialog();
                break;
        }
    }

    _keyUpOnGame() {
        this.longPressTime = 0;
    }

    _keyDownOnPlayer(para: KeyEventParameters) {
        this.longPressTime = 0;
        let player = PlayerManager.instance.controllerPlayer;
        let moveComp = player.getComponent(MoveComp);
        switch (para.DirectionCode) {
            case DirectionCode.LEFT:
                moveComp.setDirection(cc.v3(-1, 0));
                break;
            case DirectionCode.RIGHT:
                moveComp.setDirection(cc.v3(1, 0));
                break;
            case DirectionCode.UP:
                moveComp.setDirection(cc.v3(0, 1));
                break;
            case DirectionCode.DOWN:
                moveComp.setDirection(cc.v3(0, -1));
                break;
            case DirectionCode.ENTER:
                let makeHome = cc.find("Canvas/UI/安家");
                let event = new cc.Event.EventTouch([], false);
                event.getLocation = () => makeHome.convertToWorldSpaceAR(cc.Vec2.ZERO);
                event.target = makeHome;
                event.currentTarget = makeHome;
                makeHome.emit(cc.Node.EventType.TOUCH_START, event);
                makeHome && makeHome.emit(cc.Node.EventType.TOUCH_END, event);
                break;
            case DirectionCode.BACK:
                UiManager.instance.openPauseDialog();
                break;
        }
    }

    _keyUpOnPlayer(para: KeyEventParameters) {
        let player = PlayerManager.instance.controllerPlayer;
        let moveComp = player.getComponent(MoveComp);
        moveComp.setDirection(cc.v3(0, 0));
    }


}
